<template>
  <div class="app-contain">
    
    <!-- 第二行 -->
    <div class="two">
      <!-- 第一排搜索卡片 -->
    <el-card class="box-card">
      <el-row style="font-size: 15px; line-height: 2em; color: #909399" :gutter="60">
        <el-col :span="6">
          <div class="two_left">
            <div>
              {{electiveSum}} 门
              <span>我的选修</span>
            </div>
            
          </div>
        </el-col>
        
        <el-col :span="18" style="padding-left:75px;padding-right:200px">
          <el-form :model="courseFindByPage">
            <el-col :span="8">
              <div class="search">
                课程名称：<el-input v-model="courseFindByPage.etmsCourse.courseTitle" placeholder="课程名称" ></el-input>
              </div>
            </el-col>
            <el-col :span="8">
              <div class="search">
                课程类别：
              <el-select v-model="courseFindByPage.etmsCourse.courseType" placeholder="部门类别">
                <el-option v-for="dept in depts" :key="dept.key"  :label="dept.deptName" :value="dept.deptName"></el-option>
              </el-select>
              </div>
            </el-col>
            <el-col :span="8">
              <div class="search">
                选修必修：
              <el-select v-model="courseFindByPage.etmsCourse.courseRequired" placeholder="选修/必修">
                <el-option label="选修" value="选修"></el-option>
                <el-option label="必修" value="必修"></el-option>
              </el-select>
              </div>
            </el-col>
            <el-col :span="8">
              <div class="search" style="margin-left:572px;margin-top:30px">
                <el-button type="primary" icon="el-icon-search" @click="getElectiveListBySearch">搜索</el-button>
              </div>
            </el-col>
          </el-form>
          </el-col>
         
        
      </el-row>
      </el-card>
    </div>
    <div class="three">
      <el-row>
        <el-col :span="3"><div class="grid-content bg-purple"></div></el-col>
        <el-col :span="20">
          <el-col :span="24">
            <el-col :span="22" v-for="(course,index) in tableData" :key="course.index" style="margin-left:93px;margin-top:20px">
              <el-card :body-style="{ padding: '0px' }" class="card">
                <div class="img">
                <img :src="course.courseCover" height="130px" width="100%" />
                </div>
                <div style="padding: 10px; font-size: 12px" class="text">
                  <span style="font-size: 15px"
                    >{{ course.courseTitle }}
                    <br /><br />
                  <span>描述:{{
                    course.courseDescription | textHandle1(course.courseDescription)
                  }}</span><br />
                  <span>类型:{{course.courseCategory}}</span><br />
                  <span>形式:{{course.courseForm}}</span>
                   </span>
                </div>
                <div class="button">
                  <el-button 
                      size="medium"
                      style="margin-left:10px;"
                      type="success"
                      @click="handleClick(index, course.courseId)"
                    >学习</el-button>
                  <el-button 
                  size="medium "
                    type="danger"
                    @click="handleDelete(index,course.courseId)"
                  style="margin-top: 16px !important">删除</el-button>
                </div>
              </el-card>
            </el-col>
          </el-col>
        </el-col>
        <el-col :span="1"><div class="grid-content bg-purple"></div></el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import {findAllElective, findMyElectiveSum} from "@/api/user"
import {findAllUser} from "@/api/user"
import { DeleteCourseByUid } from "@/api/course"


export default {
  el: ".app-contain",
  data() {
    return {
      depts:"不限",
      tableData: [],//项目数据
      electiveSum:0,
      total:0,
        currentPage:1,
      courseFindByPage: {
          id:3,
          page:1,
          limit:5,
          etmsCourse:{
            courseTitle: "",
            courseType: "",
            createdBy: "",
            courseRequired: "选修",
          },
        },
      UseridAndCourseid: {
        uid: 3,
        cid:0,
      },
      
    };
  },
  created() {
    this.courseFindByPage.id = this.$store.getters.userId;
    this.UseridAndCourseid.uid = this.$store.getters.userId;
    this.getElectiveListBySearch();
    
  },
  methods: {
      getElectiveListBySearch(){
        findMyElectiveSum(this.courseFindByPage.id).then(
          res => {
            console.log(res);
            this.electiveSum = res.data
            this.total = res.data.total;
            this.currentPage = res.data.currentPage;
          }
        ).catch(
          err =>{
            console.log(err);
            alert("总数后端有错");
          }
        ),
        findAllElective(this.courseFindByPage).then(
          res => {
            console.log(res);
            this.tableData = res.data.list;
            console.log(this.tableData);
          }
        ).catch(
          err => {
            console.log(err);
            alert("详情后端有错");
          }
        )
        },
        handleClick(index, courseId) {
          this.$router.push({
            name: "coursePage",
            query: {
              courseId: courseId,
            },
        });
    },
    handleDelete(index,iId){
      this.UseridAndCourseid.cid = iId;
      this.UseridAndCourseid.uid = this.courseFindByPage.id;
      DeleteCourseByUid(this.UseridAndCourseid).then(
        res => {
          console.log(res);
          alert(res.data);
        }
      ).catch(
          err => {
            console.log(err);
          }
        );
      findMyElectiveSum(this.courseFindByPage.id).then(
          res => {
            console.log(res);
            this.electiveSum = res.data
            this.total = res.data.total;
            this.currentPage = res.data.currentPage;
          }
        ).catch(
          err =>{
            console.log(err);
            alert("总数后端有错");
          }
        );
        findAllElective(this.courseFindByPage).then(
          res => {
            console.log(res);
            this.tableData = res.data.list;
            console.log(this.tableData);
          }
        ).catch(
          err => {
            console.log(err);
            alert("详情后端有错");
          }
        )
      },
  },
  filters: { 
      /* 给左边的字段限制 */
  　　textHandle: function(val) {
      let textNew = val
      if (val.length > 40) {
        textNew = val.slice(0, 40) + '…'
      }
      return textNew
    },
    /* 给右边的字段限制 */
    textHandle1: function(val) {
      let textNew = val
      if (val.length > 30) {
        textNew = val.slice(0, 30) + '…'
      }
      return textNew
    },

  },
};
</script>

<style>
.course {
    float: left;
    display: flex;
    width: 200px;
    height: 250px;
    margin-left: 93px;
    margin-top: 20px;
  }
.app-contain{
    width: 100%;
    height: 2000px;
    background-color: ghostwhite;
}
.el-breadcrumb {
  font-size: 20px;
  line-height: 4;
}
.two {
  clear: both;
  margin-top: 50px;
}
.two_left{
    float: left;
    margin-left: 75px;
    width: 300px;
    height: 150px;
    background-color: deeppink;
    
}
.two_right{
    float: left;
    margin-top: 25px;
    margin-left: 10px;
} 
.three{
    clear: both;
}
.button {
  
  float: right;
  margin-top: 25px;
  margin-right: 50px;
  width: 130px;
  height: 80px;
  font-size: 1.5em;
}
.description{
    float: left;
    margin-top: 45px;
    margin-left: 30px;
}
.men{
    font-size: 40px;
			letter-spacing: 10px;
			position: absolute;
			top:50%;
			left: 30%;
}
#words{
    margin-top: 1.67em;
    margin-left: 120px;
}
.teo_left{
  height: 100%;
  background-color: yellow;
  width: 300px;
  margin-right: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.two_left > div:first-child{
  width: 100%;
  height: 150px;
  font-size: 30px;
  color: #fff;
  position: relative;
  background-image: url(../../img/背景.jpg);
  display: flex;
  align-items: center;
  justify-content: center;
}
.two_left > div:first-child > span:last-child{
  font-size: 16px;
  position: absolute;
  bottom: 10px;
  right: 10px;
}
.item{
  float: left;
  margin-left: 30px;
}
.img{
  float: left;
  width: 20%;
  height: 100%;
}
.text{
  position: absolute;
    align-self: center;
    padding: 2px 2px 2px 2px;
    box-sizing: border-box;
    width: 30%;
    margin-left: 220px;
    margin-top:10px
}
</style>